package com.upc.cajabancos.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import oracle.jdbc.driver.OracleResultSet;
import oracle.jdbc.driver.OracleTypes;

import com.upc.cajabancos.model.Banco;
import com.upc.cajabancos.model.PagoBanco;
import com.upc.cajabancos.util.DataAccessObjetc;

public class PagoBancoDAO {
	
	private Connection connection = null;
	
	public PagoBancoDAO(Connection connection){
		this.connection = connection;
	}
	
	/**
	 * Busca las pagos en banco
	 * @author Cesar Jesus
	 * @since 30/01/2011
	 * @param codBanco
	 * @param desde
	 * @param hasta
	 * @return List<PagoBanco>
	 */	
	public List<PagoBanco> buscar(String codBanco, Date desde, Date hasta) throws Exception{
		CallableStatement cstmt = null;	
		ResultSet rs = null;
		PagoBanco pagoBanco = null;
		List<PagoBanco> listaPagoBancos = new ArrayList<PagoBanco>();
    	
    	try{
			
			cstmt = connection.prepareCall(DataAccessObjetc.buildProcedureCall("BUSCAR_PAGOS",4));
			
			if("".equals(codBanco)){
				cstmt.setNull(1, OracleTypes.NULL);
			}
			else{
				cstmt.setString(1, codBanco);
			}
			if(desde == null){
				cstmt.setNull(2, OracleTypes.NULL);
			}
			else{
				cstmt.setTimestamp(2,new Timestamp(desde.getTime()));
			}
			if(hasta == null){
				cstmt.setNull(3, OracleTypes.NULL);
			}
			else{
				cstmt.setTimestamp(3,new Timestamp(hasta.getTime()));
			}
			cstmt.registerOutParameter(4, OracleTypes.CURSOR);
			cstmt.execute();
			rs = (OracleResultSet)cstmt.getObject(4);
			
			while (rs.next()){
				pagoBanco = new PagoBanco();
				pagoBanco.setBanco(new Banco());
				pagoBanco.getBanco().setNombre(rs.getString("tx_descripcion"));
				pagoBanco.setAgencia(rs.getString("no_agencia"));
				pagoBanco.setFechaPago(rs.getDate("fe_fechapago"));
				pagoBanco.setMontoPagado(rs.getDouble("ss_montopago"));

				listaPagoBancos.add(pagoBanco);
			}
			
    	}
    	catch(Exception e){
    		throw e;
    	}
    	finally {
    		DataAccessObjetc.close(null,cstmt,rs);
		}
    	
    	return listaPagoBancos;
    }

}
